package cn.backpackerxl.easyJDBC.main;

import cn.backpackerxl.easyJDBC.util.IdType;
import cn.backpackerxl.easyJDBC.util.StringUtil;

/**
 * @Author: backpackerxl
 * CreateTime: 2022/5/22
 * FileName: BasicEntity
 **/
public class BasicEntity {
    private BasicEntity() {
    }

    public static String filed(String filedName, Class<?> clazzName, boolean isKey, IdType strategy) {
        String ann = "@Id(strategy = IdType." + strategy + ")";
        return isKey ? "\t" + ann + "\n\tprivate " +
                type(clazzName.getName()) +
                " " +
                filedName +
                ";" :
                "\tprivate " +
                        type(clazzName.getName()) +
                        " " +
                        filedName +
                        ";";
    }

    public static String getMethod(String filedName, Class<?> clazzName) {
        String prefix = clazzName.equals(Boolean.class) || clazzName.equals(boolean.class) ? "is" : "get";
        return "\tpublic " +
                type(clazzName.getName()) +
                " " +
                prefix +
                StringUtil.headBig(filedName) +
                "()" +
                " {\n" +
                "\t\treturn " +
                filedName +
                ";\n" +
                "\t}";
    }

    public static String setMethod(String filedName, Class<?> clazzName) {
        return "\tpublic void set" +
                StringUtil.headBig(filedName) +
                "(" +
                type(clazzName.getName()) +
                " " +
                filedName +
                ")" +
                " {\n" +
                "\t\tthis." +
                filedName +
                " = " +
                filedName +
                ";\n" +
                "\t}";
    }

    public static String toJSON() {
        return "\t@Override\n" + "\tpublic String toString() {\n" +
                "\t\treturn JSON.stringify(this);\n" +
                "\t}";
    }


    public static String autoImport() {
        return "import cn.backpackerxl.easyJDBC.annotation.*;\n" +
                "import cn.backpackerxl.easyJDBC.util.JSON;\n" +
                "import cn.backpackerxl.easyJDBC.util.IdType;\n";
    }

    /**
     * 确定类型
     *
     * @param packName
     * @return
     */
    public static String type(String packName) {
        String[] names = packName.split("[.]");
        return names[names.length - 1];
    }

}
